#!/bin/bash

cat <<EOF
  [i]   gen bili live .txt
        gen bili live .txt
        gen bili live .txt
        gen bili live .txt
        gen bili live .txt

EOF

API_SERVER="http://x.tv.server.lan:19999"

tmp_target_file="$1"
target_file="$2"

# default_dir="/home/hbl/services/fileserver/file_server_data/box1_more_and_more"
default_dir="/home/hbl/services/f2/data/v3_tvbox_server/more_and_more"

if [ -z "$2" ];then
        #input YES to run :    bash "$0" '/root/web81/ok.live.txt.tmp' '/root/web81/ok.live.txt'
        cat <<EOF
input YES to run :    bash "$0" '$default_dir/ok.live.txt.tmp' '$default_dir/ok.live.txt'
输入 YES
EOF
        read -t 5 -p ":" tmp
        if [ "$tmp" == "YES" ];then
                bash "$0" "$default_dir/ok.live.txt.tmp" "$default_dir/ok.live.txt"
                exit 0
        fi
fi

#'搞笑段子,9419678'
#'状师】公堂上舌战群儒,11850504'

channelsDouyu=(
'纪晓岚,9071554'
'胡歌,5448413'
'金庸武侠、古装神剧,338759'
'影视剧】总有一部你喜欢的经典影视,11774356'
'牛叔】N分钟带你看完大片,2758565'
'1000部悬疑电影,9601429'
'港剧天地1,4290711'
'港剧天地2,5522351'
'港剧天地3,5261342'

'经典电影】 历届奥斯卡最佳影片盘点解说,9402110'
'小片片】国产经典剧集专属直播间,10011547'
'迟到的侦探？？,11954507'
'令狐冲，经典武侠,11741883'
'花开】射雕~靖哥哥~黄蓉~~,11651489'
'杀死燕双鹰，活捉狄仁杰！,3439096'
'台配-名蒸蛋柯南,12134650'
'柯南探案解说中,9065302'
'☆卡卡罗特☆,7286439'
'带您进入Fatal飞雪的动漫小屋,8813596'
'大秦国要搞大事,9388931'
'史君说剧】史君说尽天下好剧,9419771'
'经典高分电影,7254536'
'霍格沃茨】伟大引起嫉妒，嫉妒导致怨毒,8814650'
'刘德华】庆祝华仔出道40周年,2516864'
'步惊云】金鳞岂是池中物，一遇风云便化龙,9290755'
'使徒行者,7810803'
'重生小说合集】重生 系统 末日 仙侠,562225'
'金庸经典,12128227'
'三国,12124964'
'漫威迷集合,1713615'
'电影film,9292499'
'逃出监狱 越狱,7575350'
'【大坑】悬疑推理视频解说,9440339'
'【小叮当】童年经典动画片,9445279'
'虫哥】魔性吐槽童年回忆,8915074'
)

#
#'星爷影院,508118'
channelsDouyu=(
'胡歌,5448413'
'神乐影院-1台,85894'
'神乐影院-2台,122402'
'神乐-爱情公寓,6566658'
'金庸武侠古装神剧,338759'
'金庸经典,12128227'
'港剧天地1,4290711'
'港剧天地2,5522351'
'港剧天地3,5261342'
'庆帝,5062195'
'三国:不可能 我二弟天下无敌,2838296'
'三国,12124964'
'邵氏影院㊣版,4246519'
'4k 影院剧场,9249162'
'凌儿影院,1377142'
'吃米滴虫,315457'
'吃米滴虫2,263824'
'冰冰经典,74374'
'麓山迷踪,4505431'
'瑶瑶恐怖,2935323'
'怡寶影院,434971'
'小黛兮影,11553944'
'萌小鬼片,3928'
'可乐影院,20415'
'小宇60帧,323876'
'恐怖惊悚,96577'
'電影劇場,7575350'
'國語電影,2436390'
'豆瓣高分,8770422'
'花卷陪看,52787'
'漫威剧情,6140589'
'霍格沃茨,8814650'
'鱼宝恐怖,1165374'
'变种人片,9651304'
'喜剧电影,9292492'
'女神电影,747764'
'避风影院,9826611'
'欧美大片,2793084'
'贝爷max,4332'
'贝爷影厅,252802'
#'菜园惊险,1735337'
'星星影院,315131'
'纪晓岚,9071554'
'使徒行者,7810803'
"豆🐟🐟,#genre#"
'影视剧:总有一部你喜欢的经典影视,11774356'
'牛叔:N分钟带你看完大片,2758565'
'1000部悬疑电影,9601429'
'经典电影】 历届奥斯卡最佳影片盘点解说,9402110'
'小片片】国产经典剧集专属直播间,10011547'
#'令狐冲，经典武侠,11741883'
'杀死燕双鹰，活捉狄仁杰！,3439096'
'大秦国要搞大事,9388931'
'史君说剧:史君说尽天下好剧,9419771'
'经典高分电影,7254536'
'刘德华:庆祝华仔出道40周年,2516864'
'步惊云:金鳞岂是池中物，一遇风云便化龙,9290755'
'重生小说合集:重生 系统 末日 仙侠,562225'
'漫威迷集合,1713615'
'电影film,9292499'
'大坑】悬疑推理视频解说,9440339'
'小叮当】童年经典动画片,9445279'
'台配-名蒸蛋柯南,12134650'
'柯南探案解说中,9065302'
'卡卡罗特,7286439'
'带您进入Fatal飞雪的动漫小屋,8813596'
'虫哥】魔性吐槽童年回忆,8915074'
'JJ斗地主,488743'
)

channelsBili=(
'深挖西游原著,24151243'
'深度解析86版《西游记》,32103848'
'《西游记后传》导演剪辑版,31717589'
'如懿传,32064026'
'狄仁杰1到4部,32757450'
'神探狄仁杰第1-4部,27653605'
'铁齿铜牙纪晓岚,23333830'
'今日电影票房Top10,833792'
'YouTube沉浸式英语课堂,32128068'
'影视解说学英语,31301675'
'看剧不如学英语,27359756'
'英语这样学才有效,32174143'
'北京新东方托福GREGMAT,22346833'
'小旋风影视,31689384'
'电影迷小帅,31411252'
'物理,27634712'
'高分经典电视剧随机播,32514223'
'一口气看完海贼王1到1000集！,24273525'
'龙珠Z,23722923'
'神奇の蜡笔小新,26179557'
'名侦探柯南,25653613'
'柯南剧场版解说,7595343'
'猫和老鼠】,30401859'
'蜡笔小新,30837590'
'经典刑侦剧【技术插播,23776217'
'经典武侠【技术插播,24622261'
'寻秦记,13566606'
'林正英 经典 恐怖 女鬼,23515125'
'香港经典影视,22450222'
)



base_urls_file1="/tmp/live.douyu.url"
URL1=""
URL2="http://maomao.kandiantv.cn/douyu1.php?id="
getURL(){
    local u urls
    # base_urls_file2="/tmp/live.douyu.url2"

    echo "--"
    #grep "/douyu/[1-9][0-9]*" "$default_dir/ok.live.txt"  | sed "s/^[^,]*,//" | sed 's@[^/]*$@@' | tee "$base_urls_file1" | sort | uniq
    echo "##"
    grep "/douyu/[1-9][0-9]*" "$default_dir/ok.live.txt"  | sed "s/^[^,]*,//" | sed 's@[^/]*$@@'| sort | tee "$base_urls_file1" | uniq
    echo "--"

    urls="$(grep "/douyu/[1-9][0-9]*" "$default_dir/ok.live.txt"  | sed "s/^[^,]*,//" | sed 's@[^/]*$@@' | sort |tee "$base_urls_file1" | uniq)"

    ## 选最后的 url
    local max=0
    local url=""

    for u in $urls;do
            if ! echo "$u" | grep -i "http://";then
                continue
            fi
            # n="$(grep -F "$u" "$base_urls_file1" | wc -l)"
            n="$(grep -cF "$u" "$base_urls_file1")"
            echo "[info] u=$u, n=$n"
            if [ "$n" -gt "$max" ];then
                    max="$n"
                    url="$u"
            fi
    done

    echo "n=$max"
    echo "url=$url"
    URL1="$url"
}


gen_douyu(){
    local my_url
    echo
    echo "$1"
    my_url="$2"
    for ch in "${channelsDouyu[@]}";do
            if [  -z "$ch" ];then
                    continue
            fi
            if echo "$ch" | grep -E ",#genre# *$";then
                    continue
            fi
            echo "#ch=$ch"
            roomid="$(echo "$ch" | sed -E "s/ *$//" | grep -oE '[0-9]*$')"
            # 不要有 逗号 !! ( (单|双)引号包起来没用)
            roomname="$(echo "$ch" | sed -E 's/,[0-9]* *$//' | sed 's/,//g'|sed -E 's/(^ *)|( *$)//' )"
            echo "$roomname,$my_url$roomid"
    done
    echo
}

gen_douyu1(){
    gen_douyu "豆🐟,#genre#" "$API_SERVER/api/live/douyu/"
}

gen_douyu2_1(){
    gen_douyu "豆🐟2-1,#genre#" "$URL1"
}

gen_douyu2_2(){
    gen_douyu "豆🐟2-2,#genre#" "$URL2"
}


# gen_douyu2_x1(){
#   echo
#   echo "豆🐟2-1,#genre#"
#   my_url="$URL1"
#   for ch in "${channelsDouyu[@]}";do
#         if [  -z "$ch" ];then
#                 continue
#         fi
#         if echo "$ch" | grep -E ",#genre# *$" &>/dev/null;then
#                 continue
#         fi
#         echo "#ch=$ch"
#         roomid="$(echo "$ch" | sed -E "s/ *$//" | grep -oE '[0-9]*$')"
#         # 不要有 逗号 !! ( (单|双)引号包起来没用)
#         roomname="$(echo "$ch" | sed -E 's/,[0-9]* *$//' | sed 's/,//g'|sed -E 's/(^ *)|( *$)//' )"
#         echo "$roomname,$my_url$roomid"
#   done
#   echo
# }

# gen_douyu2_x2(){
#   echo
#   echo "豆🐟2-2,#genre#"
#   my_url="$url2"
#   for ch in "${channelsDouyu[@]}";do
#         if [  -z "$ch" ];then
#                 continue
#         fi
#         if echo "$ch" | grep -E ",#genre# *$" &>/dev/null;then
#                 continue
#         fi
#         echo "#ch=$ch"
#         roomid="$(echo "$ch" | sed -E "s/ *$//" | grep -oE '[0-9]*$')"
#         # 不要有 逗号 !! ( (单|双)引号包起来没用)
#         roomname="$(echo "$ch" | sed -E 's/,[0-9]* *$//' | sed 's/,//g'|sed -E 's/(^ *)|( *$)//' )"
#         echo "$roomname,$my_url$roomid"
#   done
#   echo
# }

gen_bili1(){
  echo
  echo "毕哩,#genre#"
  my_url="$API_SERVER/api/live/bilibili/"
  for ch in "${channelsBili[@]}";do
        echo "#ch=$ch"
        if [  -z "$ch" ];then
                continue
        fi
        roomid="$(echo "$ch" | sed -E "s/ *$//" | grep -oE '[0-9]*$')"
        # 不要有 逗号 !! ( (单|双)引号包起来没用)
        roomname="$(echo "$ch" | sed -E 's/,[0-9]* *$//' | sed 's/,//g'|sed -E 's/(^ *)|( *$)//' )"
        echo "$roomname,$my_url$roomid"
        #echo "$roomname,$url$roomid"
  done
  echo
}

# https://server.lan/noTv.mp4
noTv="http://v3.tv.server.lan:19999/files/videos/oo/noTv.mp4"
gen_split(){
    echo "==$(date '+%Y/%m/%d %H:%M')==,#genre#"
    echo "none_1,$noTv"
    echo "none_2,$noTv"
    echo "$(date '+%Y/%m/%d %H:%M'),$noTv"
    echo ""
}

##1
getURL

gened="$base_urls_file1.gened"
echo "# .. "
{
        gen_douyu1
        gen_bili1

        gen_split

        gen_douyu2_1
        gen_douyu2_2

} | tee "$gened"

echo "# ..."
if [ -n "$target_file" ];then
    (
        # 2 号 直播文件
        {
            # 去掉 注释的
            grep -vE "^ *#" "$gened"
            echo ""
        } > "$target_file.2"
    )

    (
        # 1 号 直播文件
        {
                echo ""
                cat "$tmp_target_file"
                echo ""
        } > "$target_file"
    )

else
        echo "[i] bash '$0' '$default_dir/ok.live.txt.tmp' '$default_dir/ok.live.txt'"
fi
